package 
{
	import alternativa.engine3d.controllers.CameraController;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.core.Scene3D;
	import alternativa.engine3d.display.View;
	import alternativa.utils.FPS;
	
	import alternativa.engine3d.controllers.ObjectController;
	import alternativa.engine3d.controllers.WalkController;
	import alternativa.utils.MathUtils;
	import alternativa.types.Point3D;
	
    import alternativa.utils.KeyboardUtils;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.external.ExternalInterface;
	
	[SWF(backgroundColor="#000000", frameRate="100")]

	public class Projet extends Sprite 
	{

		private var scene:Scene3D;
		private var view:View;
		private var camera:Camera3D;
		private var cameraController:CameraController;
		
		//Objet 3d
		private var etage0:Object3D;
		private var etage1:Object3D;
		private var etage2:Object3D;
		private var etage3:Object3D;
		private var etage4:Object3D;
		private var etage5:Object3D;
		
		//escalier	
		private var escalier:Object3D;
		private var escalier1:Object3D;
		private var escalier2:Object3D;
		private var escalier3:Object3D;
		private var escalier4:Object3D;
		
		//cage escalier
		private var cageescalier:Object3D;
		
		//Ascenseur
		private var ascenseur1:Object3D;
		private var ascenseur2:Object3D;
		private var skybox:Object3D;

		//Camera 
		//private var container:Object3D;
		private var controller:WalkController;
		
		
		public function Projet() {
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init(e:Event):void
		 {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.quality= StageQuality.LOW;
			
			// Creation de la scene
			scene = new Scene3D();
			scene.root = new Object3D();
			
			// Creation de la skybox
			skybox=new Skybox();
			skybox.x=2500;
			
			//Creation des ascenseurs
			ascenseur1 = new Ascenseur(1);
			ascenseur1.x=4655;
			ascenseur1.y=-150;
			ascenseur2 = new Ascenseur(2);
			ascenseur2.x=4655;
			ascenseur2.y=-350;


			//Creation de l'étage + ascenseur
			etage0 = new Etage(0);
			etage0.z=0;
			etage1 = new Etage(1);
			etage1.z=300;
			etage2 = new Etage(2);
			etage2.z=600;
			etage3 = new Etage(3);
			etage3.z=900;
			etage4 = new Etage(4);
			etage4.z=1200;
			etage5 = new Etage(5);
			etage5.z=1500;
			
			//Creation cage escalier
			cageescalier = new Cageescalier();
			cageescalier.x = 4650;
			cageescalier.y = 50;
			cageescalier.z = 0;
			
			//Ajout des objet 3d a la scene
			scene.root.addChild(skybox);
			scene.root.addChild(ascenseur1);
			scene.root.addChild(ascenseur2);
			scene.root.addChild(etage0);
			scene.root.addChild(etage1);
			scene.root.addChild(etage2);
			scene.root.addChild(etage3);
			scene.root.addChild(etage4);
			scene.root.addChild(etage5);	
			scene.root.addChild(cageescalier);
			
			//Ajout des escalier
            createescalier();
            
            //Création des Objets
            createObjet();
            
            
			var rootx:Number=root.loaderInfo.parameters.coord_x;
			var rooty:Number=root.loaderInfo.parameters.coord_y;
			var rootz:Number=root.loaderInfo.parameters.coord_z;
			// Adding camera and view
			
			camera = new Camera3D("Camera");
			camera.x = 100;
			camera.y = 200;
			camera.z = 20;
			scene.root.addChild(camera);
			
			view = new View();
			addChild(view);
			view.camera = camera;
			view.interactive = true;
			
			
			controller = new WalkController(stage);
			controller.object = camera;
			controller.lookAt(camera.coords);
			controller.speed = 500;
			controller.gravity = 500;
			controller.checkCollisions = true;
			controller.collider.radiusX = 5;
			controller.collider.radiusY = 5;
			controller.collider.radiusZ = 100;
		
			//Ajouter rootx, rooty, rootz ici sinon mettre 100 ,100 , 10 
			controller.coords = new Point3D(rootx, rooty, rootz);
			controller.onStartMoving = setLowQuality;
			controller.onStopMoving = setHighQuality;
		    controller.setDefaultBindings();
			controller.bindKey(KeyboardUtils.UP, ObjectController.ACTION_FORWARD);
			controller.bindKey(KeyboardUtils.DOWN, ObjectController.ACTION_BACK);
			controller.bindKey(KeyboardUtils.LEFT, ObjectController.ACTION_LEFT);
			controller.bindKey(KeyboardUtils.RIGHT, ObjectController.ACTION_RIGHT);

			// FPS counter init
			FPS.init(stage);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,reportKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP,reportKeyUp);
			stage.addEventListener(Event.RESIZE, onResize);
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			onResize(null);
		}

		private function onResize(e:Event):void
		{
			view.width = stage.stageWidth;
			view.height = stage.stageHeight;
		}
		
		private function onEnterFrame(e:Event):void 
		{
			ExternalInterface.call("affichCoord",camera.coords.x,camera.coords.y,camera.coords.z)
			
			// User input processing
			controller.processInput();
			// Scene calculation
			scene.calculate();
		}
		
		private function reportKeyDown(ke:KeyboardEvent):void {
			switch (ke.keyCode){
				case 17: //CTRL
					controller.enabled=false;
			}
		}
		
		private function reportKeyUp(ke:KeyboardEvent):void {
			switch (ke.keyCode){
				case 17: //CTRL
					controller.enabled=true;
					
			}
		}
		
		private function setLowQuality():void {
			stage.quality = StageQuality.LOW;
		}
		private function setHighQuality():void {
			stage.quality = StageQuality.HIGH;
		}
		
		private function createescalier():void{
			
             //creation de  l escalier
			escalier = new Escalier();
			escalier.x = 4650;
			escalier.y = 400;
            escalier.z = 0;
                	
            escalier1 = new Escalier();
			escalier1.x = 4650;
			escalier1.y = 400;
            escalier1.z = 300;
                	
            escalier2 = new Escalier();
			escalier2.x = 4650;
			escalier2.y = 400;
            escalier2.z = 600;
                	
            escalier3 = new Escalier();
			escalier3.x = 4650;
			escalier3.y = 400;
            escalier3.z = 900;
                	
            escalier4 = new Escalier();
			escalier4.x = 4650;
			escalier4.y = 400;
            escalier4.z = 1200;
                	
           //Ajout a la scene
           scene.root.addChild(escalier);
           scene.root.addChild(escalier1);
           scene.root.addChild(escalier2);
           scene.root.addChild(escalier3);
           scene.root.addChild(escalier4);
        }
        private function createObjet():void
        {
			for (var i:int; i<=5;i++)
			{
				for (var y:int = 1; y<=5; y++)
				{
					(Piece)(this.scene.root.getChildByName("Etage"+i).getChildByName("piece"+y)).addChaise();
					(Piece)(this.scene.root.getChildByName("Etage"+i).getChildByName("piece"+y)).addBureau();
				}
			}
			(Piece)(this.scene.root.getChildByName("Etage0").getChildByName("piece1")).affichDocument();
        }
	}
}